package com.atguigu.srb.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.atguigu.srb.base.exception.BusinessException;
import com.atguigu.srb.base.response.ResponseEnum;
import com.atguigu.srb.oss.config.OssProperties;
import com.atguigu.srb.oss.service.OssService;
import org.joda.time.DateTime;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;

@Service
@EnableConfigurationProperties(OssProperties.class) //指定当前组件类初始化之前必须创建OssProperties对象注入到容器中
public class OssServiceImpl implements OssService {

    @Override
    public String upload(MultipartFile file, String module) {
        //1、上传文件在oss桶中保存的路径：
        String objectName = module+new DateTime().toString("/yyyy/MM-dd/")+System.currentTimeMillis()
                +"_"+ UUID.randomUUID().toString().substring(0,6)
                + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(OssProperties.SCHEMA+OssProperties.ENDPOINT,
                    OssProperties.ACCESS_KEY_ID, OssProperties.ACCESS_KEY_SECRET);
        try {
            InputStream inputStream = file.getInputStream();
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(OssProperties.BUCKET_NAME,
                        objectName, inputStream);
            // 创建PutObject请求。
            ossClient.putObject(putObjectRequest);
            String imgUrl = OssProperties.SCHEMA+OssProperties.BUCKET_NAME+
                    "."+OssProperties.ENDPOINT+"/"+objectName;
            return imgUrl;
        } catch (Exception ce) {
            System.out.println("Error Message:" + ce.getMessage());
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
    // 前端可以多传一个参数：module  idCard1
    @Override
    public void delete(String path) {
        // 获取文件访问地址中的 资源完整路径 不包含服务器地址
        //"https://sh230201-osstest.oss-cn-shanghai.aliyuncs.com/"
        String objectName = path.substring((OssProperties.SCHEMA+OssProperties.BUCKET_NAME
                +"."+OssProperties.ENDPOINT+"/").length());
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(OssProperties.SCHEMA+OssProperties.ENDPOINT,
                OssProperties.ACCESS_KEY_ID, OssProperties.ACCESS_KEY_SECRET);

        try {
            // 删除文件或目录。如果要删除目录，目录必须为空。
            ossClient.deleteObject(OssProperties.BUCKET_NAME, objectName);
        } catch (Exception ce) {
            System.out.println("Error Message:" + ce.getMessage());
            throw new BusinessException(ResponseEnum.OSS_DELETE_ERROR);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
